<?php
    if ($_FILES["host_file"]["error"] > 0) {
        die("Error on upload file: " . $_FILES["file"]["error"] . "<br />");
    }
    elseif ($_FILES["host_file"]["size"] == 0 || $_FILES["host_file"]["size"] == "") // if no any file selected
		die("No file selected");
	elseif (strtoupper(substr($_FILES['host_file']['name'], -3, 3)) != "XLS") // if not Excel file
	    die("Wrong file format, only XLS is supported");
	else {
	    $error = '';
	    require_once '../Excel/reader.php';
		//$cpe_tmp_file = "D:\\liepixie\\Desktop\\2.xls";
		try {
			$cpe_tmp_file = $_FILES['host_file']['tmp_name'];
		    $data = new Spreadsheet_Excel_Reader();
            $data->setOutputEncoding('UTF-8');
            $data->read($cpe_tmp_file);
            $array = array();
		}
		catch (Exception $e) {
		    $error = $error . $e->getMessage() . '<br />';
			die($error);
		}
	}
?>
	
<?php
	$row_number = $data->sheets[0]['numRows'];
	if ($row_number < 1) {
		    die("No any data found in configuration file");
	}
?>


<?php
    include "../db/db_connect.php";
	
	$error = "";   
	
	for ($row = 1; $row <= $row_number; $row++) {
	    $first_cell_value = $data->sheets[0]['cells'][$row][1];
		if (empty($first_cell_value) || $row == 1) {
		    continue;
		}
			
		$host_ip = "\"{$data->sheets[0]['cells'][$row][1]}\"";
		$host_name = "\"{$data->sheets[0]['cells'][$row][2]}\"";
		$host_location = "\"{$data->sheets[0]['cells'][$row][3]}\"";
		$host_username = "\"{$data->sheets[0]['cells'][$row][4]}\"";
		$host_password = "\"{$data->sheets[0]['cells'][$row][5]}\"";
		$host_os = "\"{$data->sheets[0]['cells'][$row][6]}\"";
		$host_hdd = "\"{$data->sheets[0]['cells'][$row][7]}\"";
		$host_memory = "\"{$data->sheets[0]['cells'][$row][8]}\"";
		$host_cpu = "\"{$data->sheets[0]['cells'][$row][9]}\"";
		$host_shared_path = "\"{$data->sheets[0]['cells'][$row][10]}\"";
		$host_shared_name = "\"{$data->sheets[0]['cells'][$row][11]}\"";
		
		$qry = "select host_ip from host_view where host_ip={$host_ip}";
	    $ds  = mysql_query($qry, $con);
		$number = mysql_num_rows($ds);
		if ($number == 0) { // new host
		    $insert_command = "insert into host (host_ip, user_id, status) values ({$host_ip}, 1, 0)";
			if (mysql_query($insert_command, $con)) {
			    $columns = "(host_ip, host_name, username, password, location, " .
				           "os, cpu, memory, harddisk, " .
						   "shared_path, shared_name)";
				$values = "values ({$host_ip}, {$host_name}, {$host_username}, {$host_password}, {$host_location}, " .
				                   "{$host_os}, {$host_cpu}, {$host_memory}, {$host_hdd}, " .
								   "{$host_shared_path}, {$host_shared_name})";
			    $insert_command = "insert into host_info {$columns} {$values}";
				if (! mysql_query($insert_command, $con)) {
				    $error = $error . mysql_error();
				}
			}
			else {
			    $error = $error . mysql_error();
			}
		}
		else {
		    $update_command = "update host_info set host_name={$host_name}, username={$host_username}, password={$host_password}, location={$host_location}, " .
			                                       "os={$host_os}, cpu={$host_cpu}, memory={$host_memory}, harddisk={$host_hdd}, " .
												   "shared_path={$host_shared_path}, shared_name={$host_shared_name} " .
												"where host_ip={$host_ip}";
			if (! mysql_query($update_command, $con)) {
				    $error = $error . mysql_error();
				}
		}

		echo $error;
    }

	include "../db/db_close.php";
	
	if ($error != "") {
	    echo $error;
	}
	else {
	    $target_file_name = "host_configration_".date('YmdHis').".xls";
	    move_uploaded_file($_FILES["host_file"]["tmp_name"], "../../upload/{$target_file_name}");
		copy("../../upload/{$target_file_name}", "../../upload/host_configuration.xls");
	    echo "All the data upload successful";
	    header('Location: ../../index.php');
	}
?>